import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
//新版路由路径
Vue.use(VueRouter)

const routes = [{
    path: '/login',
    component: Login
}, {
    path: '/home',
    component: Home
}, {
    path: '/',
    redirect: '/login'
}]

const router = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })
    //挂载路由导航守卫
    //to代表将要访问的路径
    //from代表从哪个路径而来例如/login
    //next代表放行函数next（“/login”）代表强制跳转
router.beforeEach((to, from, next) => {
    if (to.path == '/login') return next();
    //获取token
    const tokenstr = window.sessionStorage.getItem('token')
    if (!tokenstr) return next('/login'); //根据token值来判断，如果为空，则返回登录页面

    next(); //否则放行
})
export default router